const express = require('express')
const router = express.Router()
const userDb = require('../db/user-db')
const {logI} = require('../log.js')
const {retError, retSuccess} = require('../utils/ret')
const checkType = require('../utils/checkType')
const checkToken = require('../utils/checkToken')
const checkDeviceNo = require('../utils/checkDeviceNo')
const moment = require('moment')

router
    .post('/', async(req, res) => {
        let {body: {maddr,deviceNo,token,QRCodeUUID}} = req
        logI("A-S", maddr, "scan_code", `${JSON.stringify(req.body)}`, "null")
        try{
            const error = checkType.scanCode(req.body)
            if(error){
                retError("scan_code",maddr, 10001,  error.details[0].message, res);
            }else{
                let msg = {};
                msg = await checkToken(token,maddr);
                if(msg.code === 0){
                    const deviceInfo={
                        'maddr':maddr,
                        'deviceNo':deviceNo
                    }
                    let device={};
                    device = await checkDeviceNo(deviceInfo);
                    if(device.code === 0){
                        let ret={};
                        ret = await userDb.selectQRCode(QRCodeUUID);
                        console.log(ret)
                        if(!ret){
                            retError("scan_code", maddr,10016, 'Invalid QR code', res);
                        }else{
                            if(ret.receiver===null){
                                if(maddr === ret.maddr){
                                    retError("scan_code", maddr,10018, 'The building has been bound', res);
                                }else{
                                    let result = {};
                                    result = await userDb.selectRole(maddr,ret.buildingUUID);
                                    if(!result){
                                        const time = moment().format('YYYY-MM-DD HH:mm:ss');
                                        await userDb.updateQRCode(maddr,time,QRCodeUUID);
                                        await userDb.insertBuildingMaddrScan(maddr,ret.buildingUUID,ret.role,time,ret.role,time);
                                        await userDb.insertMeshMaddrInfoScan(maddr,ret.buildingUUID,time,ret.maddr,ret.buildingUUID,time);
                                        retSuccess("scan_code", maddr,0, "", res);
                                    }else{
                                        retError("scan_code", maddr,10018, 'The building has been bound', res);
                                    }
                                }
                            }else{
                                retError("scan_code", maddr,10017, 'The code has already been used', res);
                            }
                        }
                    }else{
                        retError("scan_code", maddr,device.code, device.body, res);
                    }
                }else{
                    retError("scan_code", maddr,msg.code, msg.body, res);
                }
            }
        } catch (e) {
            retError("scan_code",maddr, 10001, e, res);
        }
    })
module.exports = router
